package com.ld.admin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.service.user.UserService;
import com.ld.shieldsb.annotation.duplicate.SubmitRequestIntercept;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.web.util.Web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "登录")
@Controller
@RequestMapping("${shieldsb.adminPath}")
public class LoginController extends AdminBasicController<Object> {
    @Autowired
    protected UserService userService;

    @ApiOperation("登录页面")
    @GetMapping(path = "/toLogin")
    public String toLogin() {
        if (Web.Util.getSessionUser(request) != null) {
            return "redirect:" + adminPath + "/"; // 已经登录跳转首页
        }
        return "a/login";
    }

    @ApiOperation(value = "登录操作", notes = "提交登录")
    @RequestMapping(path = "/login", method = RequestMethod.POST)
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object login(@RequestParam String userName, @RequestParam String password) {
        Integer rememberMe = getInt("rememberme");
        if (rememberMe == 1) { // 记住账号，session过期时间改为一月
            rememberMe = 60 * 60 * 24 * 30;
        } else {
            rememberMe = null;
        }
        Integer tempAccount = getInt("tempaccount"); // 临时账号
        Result result = userService.login(request, userName, password, rememberMe, tempAccount == 1 ? true : false);
        return result;
    }

    @ApiOperation("退出登录")
    @GetMapping(path = "/logout")
    public String logout() {
        Result result = userService.loginout(request);
        return (String) result.getData();
    }

}
